using UnityEngine;
using QFramework;

namespace XCZProject
{
	public partial class EnemyFly : ViewController,IEnemy
	{
		public float HP = 3;
		
		public float MovementSpeed = 2.0f;
		void Start()
		{
			EnemyGenerator.EnemyCount.Value++;
		}

		private void FixedUpdate()
		{
			if (Player.Default)
			{	
				// 获取玩家当前方向
				var direction = (Player.Default.transform.position - transform.position).normalized;
			
				// 向玩家方向移动
				//transform.Translate(direction*Time.fixedDeltaTime*MovementSpeed);
				// 用刚体触发伤害事件，避免用上面这个代码，容易在接触到敌人时触发多次伤害
				SelfRigidbody2D.velocity = direction * MovementSpeed;
			}
			else
			{
				SelfRigidbody2D.velocity = Vector2.zero;
			}

		}

		private void Update()
		{
			if (HP<=0)
			{
				//UIKit.OpenPanel<UIGamePassPanel>();
				// TODO：添加经验值调用
				// print(gameObject.name);
				Global.GeneratePowerUp(gameObject,false);
				this.DestroyGameObjGracefully();
				//Global.Exp.Value++;
			}
		}

		private bool mIgnoreHurt = false;
		
		/// <summary>
		/// 敌人受伤时调用
		/// </summary>
		/// <param name="value"></param>
		public void Hurt(float value,bool force=false,bool cirtical=false)
		{
			if (mIgnoreHurt&&!force)
			{
				return;
			}
			
			FloatingTextController.Play(transform.position+Vector3.up*1,value.ToString(),cirtical);
			
			Sprite.color=Color.red;
			AudioKit.PlaySound("hit");
			ActionKit.Delay(0.4f,() =>
			{
				// 敌人的血量每次减少的数量取决于主角当前的攻击力
				this.HP -= value;
				this.Sprite.color = Color.white;
				mIgnoreHurt = false;
			}).Start(this);
		}

		private void OnDestroy()
		{

			EnemyGenerator.EnemyCount.Value--;
		}

		public void SetSpeedScale(float speedScale)
		{
			MovementSpeed *= speedScale;
		}

		public void SetHpScale(float hpScale)
		{
			HP *= hpScale;
		}
	}
}
